<?php

namespace app\admin\controller;
use app\admin\controller\Base;

/**
 * 卡密控制器
 */
class Card extends Base
{

    //卡密添加
    public function cardadd()
    {
        
        # code...
        if (request()->isAjax()) {
            $cardnumber=input('post.cardnumber',1);
            $cardtype_id=input('post.cardtype_id');
            $cardtypes=db('Cardtype')->where('id', $cardtype_id)->find();
            $softwares=db('Software')->where('id', $cardtypes['cardtypesoftware'])->find();
            $cardtype_key=$cardtypes['cardtype_key'];
            $softwares_key=$softwares['software_key'];
            $list = [];
            do
            {
                do
                {
                    $card_key = $softwares_key.'-'.$cardtype_key.'-'.bin2hex(random_bytes(9));
                
                }
                while (db('card')->where('cardname', $card_key)->find()); 
                          
                $data=[
                    'cardname'=>$card_key,
                    'cardtype_id'=>$cardtype_id
                ];               
                $cardnumber=$cardnumber-1;  
              
                $list[]= $data;
            }while ($cardnumber>0);
            
            $result=model('Card')->add($list);
            if ($result==1) {
                $this->success('卡密添加成功','admin/card/cardlist');
            }else{
                $this->error($result);
            }
        }


        $cardtypes=model('Cardtype')->select();
        $viewData=[
            'cardtypes'=>$cardtypes
        ];
        $this->assign($viewData);
        return view();
    }

    //卡密列表
    public function cardlist()
    {       
        # code...
        #->where('id',73)
        $cardtypesvalue='0';
        $starvalue=false;
        $result=db('Cardsystem')->where('id', 1)->find();
        $cardtypesvalue=$result['cardtypesvalue'];
        if ($result['starvalue']=='1') {
            $starvalue=true;
        }
        if (request()->isAjax()) {
            $cardtypesvalue=input('post.cardtypesvalue',0);
            $starvalue=input('post.startext');
           
            if ($starvalue=='true') {
                $starvalue='1';
            }else{
                $starvalue='0';
            }
           
            $result= db('Cardsystem') ->where('id',1)->update(['cardtypesvalue'=>$cardtypesvalue,'starvalue'=>$starvalue]);
           
            if ($result==1) {
                $this->success('卡密查询成功','admin/card/cardlist');
            }else{
                $this->error('卡密查询重复','admin/card/cardlist');
            }
        }
        if ($cardtypesvalue=='0') {
            if ($starvalue) {
                //没有启用,全部
                $cards=model('Card')->where('cardstar_time','null')->with('Cardtype,Cardtype.software')->order('id', 'asc')->paginate(50);
            }else{
                //所有
                $cards=model('Card')->with('Cardtype,Cardtype.software')->order('id', 'asc')->paginate(50);
            }
           
        }else{
            if ($starvalue) {
                //单个 没有启用
                $cards=model('Card')->where('cardtype_id',$cardtypesvalue )->where('cardstar_time','null')->with('Cardtype,Cardtype.software')->order('id', 'asc')->paginate(50);
            }else{
                 //单个 所有
                 $cards=model('Card')->where('cardtype_id',$cardtypesvalue)->with('Cardtype,Cardtype.software')->order('id', 'asc')->paginate(50);
            }
            
        }
       
        $cardtypes1=model('Cardtype')->select()->toArray();     
        $cardtypes[0]= [          
              'id' => '0',
              'cardtypename'=>'全部'        
              ] ;
             
        $cardtypes = array_merge($cardtypes, $cardtypes1);
        $viewData=[
           'cardtypesvalue' =>$cardtypesvalue,
           'startext' =>$starvalue,
            'cardtypes'=>$cardtypes,
            'cards'=>$cards
        ];
        $this->assign($viewData);
        return view();
    }

   public function carddel()
   {
       # code...
       $cardInfo=model('Card')->find(input('post.id'));
       $result=$cardInfo->delete();
       if($result){
           $this->success('卡密删除成功','admin/card/cardlist');
       }
       else{
           $this->error($result);
       }
   
   }

   public function cardExcel()
   {

    $cardtypesvalue='0';
        $starvalue=false;
        $result=db('Cardsystem')->where('id', 1)->find();
        $cardtypesvalue=$result['cardtypesvalue'];
        if ($result['starvalue']=='1') {
            $starvalue=true;
        }
        if ($cardtypesvalue=='0') {
            if ($starvalue) {
                //没有启用,全部
                $cards=model('Card')->where('cardstar_time','null')->select()->toArray();
            }else{
                //所有
                $cards=model('Card')->select()->toArray();
            }
           
        }else{
            if ($starvalue) {
                //单个 没有启用
                $cards=model('Card')->where('cardtype_id',$cardtypesvalue )->where('cardstar_time','null')->select()->toArray();
            }else{
                 //单个 所有
                 $cards=model('Card')->where('cardtype_id',$cardtypesvalue)->select()->toArray();
            }
            
        }
    
       
       # code...
       $titlname    =    ['id','卡号','卡种的id','开始时间','结束时间','机器码','创建时间','修改时间','7'];
       
        exportExcel($titlname,  $cards ,'Sheet1','卡密.xlsx');
   }

    public function cardedit()
    {
        # code...
        $cardtypeInfo=model('Cardtype')->find(input('id'));        
        $softwares=model('Software')->select();
        
        $viewData=[
            'cardtypeInfo'=>$cardtypeInfo,
            'softwares'=>$softwares

        ];
        
        $this->assign($viewData);
        return view();
    }

}